/**
 * 
 */
package bancosys.tec.services.implementor.descriptor;


import jmine.tec.di.resolver.ObjectResolver;
import jmine.tec.di.scope.TypedScope;
import jmine.tec.di.wrapper.Wrapper;
import bancosys.tec.persist.Persistable;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BaseDAOFactory;

/**
 * Resolve {@link BaseDAO}
 * 
 * @author MarceloT
 */
public class FixedBaseDAOResolver implements ObjectResolver {

    private BaseDAOFactory daoFactory;

    private final Class<?> componentType;

    private Object dao;

    /**
     * C'tor
     * 
     * @param componentType o tipo de {@link BaseDAO}
     * @param factory o {@link BaseDAOFactory}
     */
    FixedBaseDAOResolver(BaseDAOFactory factory, Class<?> componentType) {
        this.componentType = componentType;
        this.daoFactory = factory;
    }

    /**
     * C'tor
     * 
     * @param componentType o tipo de {@link BaseDAO}
     */
    FixedBaseDAOResolver(Class<?> componentType) {
        this(null, componentType);
    }

    /**
     * @return {@link BaseDAOFactory}
     */
    protected BaseDAOFactory getDaoFactory() {
        return this.daoFactory;
    }

    /**
     * @param properties {@link TypedScope}
     * @return boolean
     */
    public boolean isResolvable(TypedScope properties) {
        return true;
    }

    /**
     * @param declaredRawType type
     * @return boolean
     */
    public boolean isTypeCompatible(Class<?> declaredRawType) {
        return this.componentType == declaredRawType;
    }

    /**
     * @param properties {@link TypedScope}
     * @param wrapper {@link Wrapper}
     * @param requiredComponentClass type
     * @return {@link BaseDAO}
     */
    public Object resolveObject(TypedScope properties, Wrapper wrapper, Class<?> requiredComponentClass) {
        return this.getDao();
    }

    /**
     * @return {@link Object}
     */
    @SuppressWarnings("unchecked")
    public Object getDao() {
        if (this.dao == null) {
            this.dao = this.getDaoFactory().getDAOByClass((Class<BaseDAO<Persistable>>) this.componentType);
        }
        return this.dao;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isStatic() {
        return true;
    }

}